<template>
  <div class="goods-image">
    <!-- 大图 -->
    <div v-show="show"
         :style="[{backgroundImage: `url(${images[curIndex]})`}, largePosition]" class="large"></div>

    <!-- 中图 -->
    <div class="middle">
      <!-- 这里加了 v-lazy 下面的 @mouseenter事件就失效了?, 这是为什么 -->
      <img ref="target" :src="images[curIndex]" alt="">
      <!-- 遮罩层 -->
      <div v-show="show" ref="layerRef" :style="layerPosition" class="layer"></div>
    </div>
    <!-- 小图 -->
    <ul class="small">
      <li v-for="(img, i) in images"
          :key="img"
          :class="{active: i === curIndex}">
        <img :src="img" alt="" @mouseenter="curIndex = i">
      </li>
    </ul>
  </div>
</template>

<script>
import { reactive, ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'

export default {
  name: 'GoodsImage',
  props: {
    images: {
      type: Array,
      default: () => []
    }
  },
  setup(props) {
    // 当前预览图的索引
    const curIndex = ref(0)
    const target = ref(null)
    // 遮罩层
    const layerRef = ref(null)

    // 1.是否显示遮罩层和大图
    const show = ref(false)
    // 2.遮罩的坐标(样式)
    const layerPosition = reactive({
      left: '0',
      top: '0'
    })
    // 3.大图背景定位(样式)
    const largePosition = reactive({
      backgroundPositionX: '0',
      backgroundPositionY: '0',
    })

    // 4.使用useMouseInElement得到基于元素的左上角坐标和是否离开元素
    const {
      isOutside,
      elementX,
      elementY
    } = useMouseInElement(target)

    // 使用监听器方式,   监听器 ==> 监听事件???
    // 当监听的数据是ref/ reactive, 则不需要写成函数返回该属性的方式
    watch([isOutside, elementX, elementX], () => {
        const layerWidth = layerRef.value.clientWidth  // 200
        const halfLayerW = layerWidth / 2              // 100
        const midImgWidth = target.value.clientWidth   // 400
        const number = midImgWidth - layerWidth        // 200

        // 根据得到数据设置样式和是否显示遮罩层
        show.value = !isOutside.value
        // 计算坐标
        const position = {
          x: 0,
          y: 0
        }
        // 第一个判断, 可以让鼠标移动到遮罩层 x中间,
        // 中间那个判断, 让遮罩层固定靠着图片右边
        // 最后一个判断, 控制移动中鼠标保持在遮罩层 x中间
        if (elementX.value < halfLayerW) {
          position.x = 0
        } else if (elementX.value > midImgWidth - halfLayerW) {
          position.x = number
        } else {
          position.x = elementX.value - halfLayerW
        }

        if (elementY.value < halfLayerW) {
          position.y = 0
        } else if (elementY.value > midImgWidth - halfLayerW) {
          position.y = number
        } else {
          position.y = elementY.value - halfLayerW
        }
        // 给样式赋值
        layerPosition.left = position.x + 'px'
        layerPosition.top = position.y + 'px'
        largePosition.backgroundPositionX = -2 * position.x + 'px'
        largePosition.backgroundPositionY = -2 * position.y + 'px'
      }
    )

    return {
      curIndex,
      target,
      layerRef,
      show,
      layerPosition,
      largePosition,
      elementX,
      elementY
    }
  }
}
</script>

<style lang="less" scoped>
.goods-image {
  display: flex;
  width: 480px;
  height: 400px;
  // 子绝父相, 为后期的图片放大镜做准备的
  position: relative;

  .large {
    position: absolute;
    top: 0;
    left: 412px;
    z-index: 999;
    width: 400px;
    height: 400px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    // 这个是将图片放大两倍
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }

  .middle {
    width: 400px;
    height: 400px;
    background-color: #fff;
    position: relative;
    cursor: move;

    .layer {
      position: absolute;
      //z-index: 99;
      top: 0;
      left: 0;
      width: 200px;
      height: 200px;
      background-color: rgba(0, 0, 0, 0.2);
    }

  }

  .small {
    width: 80px;

    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;

      &.active, &:hover {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
